<template>
  <main-box>
    <PlusSearch
      v-model="queryform"
      :columns="formConfig"
      :show-number="2"
      label-width="80"
      label-position="right"
      class="mb-xs"
      @search="initData()"
      @reset="initData()"
    />
    <div class="mb-2">
      <btnExport
        :exportApi="exportApi"
        :queryform="getQueryParam()"
        download-name="管理端操作日志"
      />
    </div>
    <PureTable
      ref="tableRef"
      border
      adaptive
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :pagination="pagination"
      @page-size-change="initData()"
      @page-current-change="initData(false)"
    />
  </main-box>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { useTableConfig } from "@/utils/tableConfig";
import { type PlusColumn, PlusSearch } from "plus-pro-components";
import btnExport from "@/components/btnExport/index.vue";

import { listApi, exportApi } from "@/api/logs/monitorLog";

/* 查询表单 */
const formConfig: PlusColumn[] = [
  {
    label: "时间",
    prop: "time",
    valueType: "date-picker",
    fieldProps: {
      type: "daterange",
      valueFormat: "YYYY-MM-DD"
    }
  },
  {
    label: "操作人",
    prop: "searchValue"
  }
];

const queryform = ref({ time: [] as any, searchValue: "" });

const getQueryParam = () => {
  const params = { ...queryform.value } as any;
  if (params.time && params.time.length) {
    params.params = {
      startTime: params.time[0],
      endTime: params.time[1]
    };
  }
  delete params.time;
  return params;
};

/* 表格数据 */
const { pagination } = useTableConfig();
const tableData = ref([]);
const loading = ref(false);
const columns = [
  { prop: "operTime", label: "时间" },
  { prop: "warehouseName", label: "工具室" },
  {
    prop: "userNickName",
    label: "操作人",
    formatter: row => row.userNickName ?? row.userName
  },
  {
    prop: "description",
    label: "操作详情"
  }
];

const initData = async (needInit = true) => {
  loading.value = true;
  if (needInit) {
    pagination.currentPage = 1;
  }
  await listApi({
    pageNum: pagination.currentPage,
    pageSize: pagination.pageSize,
    ...getQueryParam()
  }).then(res => {
    tableData.value = res.rows;
    pagination.total = Number(res.total);
  });
  loading.value = false;
};

initData();
</script>
